package domain;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.Charset;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;

public class GetNown {
	public static void main(String[] args) {
		String content = "";
		String name_domain;
		try {

			int page=0;
			DBCollection coll = Data.data("noun");
			String url="http://vi.wiktionary.org/w/index.php?title=Th%E1%BB%83_lo%E1%BA%A1i:Danh_t%E1%BB%AB_ti%E1%BA%BFng_Anh&pagefrom=absolute+file+specifier#mw-pages";
			while(1==1)
			{
				content = "";
				URL dataURL = new URL(url);
				
				page++;
				
				HttpURLConnection connection = (HttpURLConnection) dataURL.openConnection();
				connection.setReadTimeout(500000);
				connection.setConnectTimeout(1000000);
				BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), Charset.forName("UTF8")));
				while(1==1)
				{
					String str = reader.readLine();
					if(str==null) break;
					content+=str;
					
				}
				
				Document doc = Jsoup.parse(content);
				
				url = "http://vi.wiktionary.org"+doc.select("#mw-pages a").last().attr("href");
				System.out.println(url);
				
				
				
				try {
					for(int i=0;i<doc.select(".mw-content-ltr li a").size();i++)
					{
						if(i!=0)
						{
							System.out.println(doc.select(".mw-content-ltr li a").get(i).text());
							name_domain = doc.select(".mw-content-ltr li a").get(i).text().replaceAll("[,]+", "");
							
							DBObject db = coll.findOne(new BasicDBObject("name",name_domain));
							if(db==null)
							{
								BasicDBObject ins = new BasicDBObject();
								ins.put("name", name_domain);
								coll.insert(ins);
							}
						}
					}
				} catch (Exception e) {
					// TODO: handle exception
				}
				if(!doc.select("#mw-pages a").last().text().equals("200 mục sau"))
				{
					break;
				}
			}
		} catch (Exception e) {
			
			e.printStackTrace();
		}
	}
}
